﻿<%@ Page Language="C#" Title="编辑分类" AutoEventWireup="true" CodeBehind="ItemSeller_listcate.aspx.cs" Inherits="map.member.dialogs.ItemSeller_listcate" %>

<%@ Register Assembly="RuleCode.Common" Namespace="RuleCode.Controls" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <style type="text/css">
        .mainitemcateDiv
        {
            width:820px;
        }
        .normalButton
        {
            border:none;
        }
        .child_list_td0
        {
            width:20%;
            text-align:left;
            background:url(../../img/cat-sub.png) no-repeat -20px -8px;
            text-indent:25px;
        }
        .list_td0
        {
            width:20%;
            text-align:left;
        }
        .list_td1
        {
            width:15%;
        }
        .list_td2
        {
            width:4%;
            text-align:center;
        }
        .list_td3
        {
            width:10%;
            text-align:left;
        }
        .list_td4
        {
            width:10%;
            text-align:left;
            
        }
        .list_td5
        {
            width:10%;               
            text-align:left;
        }
        .list_td6
        {
            width:15%;
            vertical-align:top;
        }
        .list_td7
        {
            width:15%;
            text-align:left;
            
        }
         .itemcateContentDiv
        {
            width:100%;
            border-top:solid 1px #B2D1FF;
            margin-top:20px;
        }
        .itemcateContentHeaderDiv
        {
            border-bottom:solid 1px lightGray;
            background-color:#F2F8FF;
            height:28px;
            font-size:small;
            vertical-align:middle;
        }
        .itemcateContentBodyDiv
        {
            width:100%;
        }
        .itemcateFooterDiv
        {
            width:100%;
            text-align:left;
            font-size:small;
            border-top:solid 1px lightGray;
            border-bottom:solid 1px #B2D1FF;
            height:31px;
        }
        .NoDataDiv
        {
            font-size:small;
            text-align:left;
            height:25px;
            line-height:25px;
            vertical-align:middle;
        }
        .itemcateFooterDiv
        {
            width:100%;
            text-align:left;
            font-size:small;
        }
        .add-button
         {
             line-height:23px;
             height:23px;
             background:url(../../img/add-button.png) no-repeat;
             font-size :12px;
             float:left;
             text-indent:35px;
             width:127px;
             text-decoration:none;
             margin-top:4px;
         }
         .hiddenObject
         {
             visibility:hidden;
         }
         .add_sub_cate
         {
             background:url(../../img/dialog-operate-complex.png) no-repeat 0px -80px;
             float:left;
             text-indent:15px;
             font-size:small;
             padding-top:3px;
             font-style:normal;
             text-decoration:none;
         }
         a.add_sub_cate_mouseover
         {
             background:url(../../img/dialog-operate-complex.png) no-repeat 0px 0px;
             cursor:pointer;
              text-decoration:none;
         }
         span.add_sub_cate_mouseover_0
         {
             background:url(../../img/dialog-operate-complex.png) no-repeat 100% -20px;
             text-decoration:none;
         }
         .list_td1 span
         {    
             padding-left:5px;
             padding-right:5px;
             float:left;
         }
         .list_td1 a
         {
             float:left;
             height:20px; 
         }
         .ItemCateOprButton
         {
             background:url(../../img/dialog-operate.png) no-repeat -23px -34px; 
             float:left; 
             display:block; 
             text-indent:-1000px; 
             overflow:hidden;
             width:23px;
             height:17px;
         }
         #addItemCateDiv,#addChildItemCateDiv
         {
             width:100%;
             background-color:Aqua ;
         }
         .saveNewCateButton
         {
             display:none;
         }
         .alertText
         {
             border:solid 1px Red;
         }       
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div id="mainitemcateDiv" class="mainitemcateDiv">   
             <div class="itemcateContentDiv">
                  <div class="itemcateContentHeaderDiv">
                    <table style =" margin-top:5px; width:100%;">
                        <tr>
                            <td class="list_td0">分类名称</td>
                            <td class="list_td1">添加子分类</td> 
                            <td class="list_td2"></td>
                            <td class="list_td3">上移</td>
                            <td class="list_td4">下移</td>                                
                            <td class="list_td5">删除</td>
                            <td class="list_td6">查看分类下的宝贝</td>
                            <td class="list_td7">移动到该分类的下方</td>
                        </tr>
                    </table>
                  </div>
                  
                  <div class="itemcateContentBodyDiv">
                     <div class="NoDataDiv" id="NoDataDiv" runat="server">
                        对不起，暂无宝贝分类！
                     </div>
                      <asp:DataList ID="MainDataList" DataKeyField="RECID" Width="100%" 
                          runat="server" onitemdatabound="MainDataList_ItemDataBound" 
                          ondatabinding="MainDataList_DataBinding">
                         <ItemTemplate>
                            <div id='<%#Eval("taobaocid").ToString ()+"_mainDataList"%>'  style=" width:100%; margin:2px 0px 2px 0px; border-bottom:dashed 1px lightGray">
                                <input id="taobaoCidHidden"  runat="server" type="hidden" value='<%#Eval("taobaocid").ToString() %>'/>
                                <table width="100%" >
                                    <tr>
                                        <td class="list_td0" style =" font-size:small;">
                                            <a title="修改分类" href ="#4" id='<%#Eval("taobaocid").ToString ()%>'  onclick ="showRenameCateName(this);"><img   style=" border:none" src ="../../img/edititem.gif"/></a>
                                            <span><%# Eval("taobaoname").ToString () %></span>
                                            <input name="editCateNameTextInput" id='<%#Eval("taobaocid").ToString ()%>' title="回车键保存" type="text" value ="" style =" display:none;"/>                                            
                                        </td>
                                        <td class="list_td1">
                                            <a  id='<%#Eval("taobaocid").ToString () %>' onclick ="showSecondAddNewCateIdDiv(this.id);"  href ="#3" style="text-decoration:none;" onmouseout="this.className='';this.getElementsByTagName('span')[0].className='';" onmouseover ="this.className='add_sub_cate_mouseover';this.getElementsByTagName('span')[0].className='add_sub_cate_mouseover_0';">
                                                <span ><em   class="add_sub_cate">添加子分类</em></span>
                                            </a>
                                        </td> 
                                        <td class="list_td2"></td>
                                        <td class="list_td3">                                                        
                                            <%#getMoveUpAnchorHTML(Convert.ToInt32(Eval("taobaosort_order")), Eval("taobaocid").ToString(), Eval("taobaoparentcid").ToString())%>
                                        </td>
                                        <td class="list_td4">
                                            <%#getMoveDownAnchorHTML(Convert.ToInt32(Eval("taobaosort_order")), Eval("taobaocid").ToString(), Eval("taobaoparentcid").ToString())%>
                                        </td>                                
                                        <td class="list_td5">
                                            <a href="#" id='<%#Eval("taobaocid").ToString() %>' title="删除" onclick ="deleteCateId(this);" onmouseover="this.style.backgroundPosition='0px 0px';" onmouseout ="this.style.backgroundPosition='-23px 0px';" class="ItemCateOprButton" style=" background-position:-23px 0px;"></a>
                                        </td>
                                        <td class="list_td6" >
                                            <a id="cateitemsAnchor" runat="server" href='<%#"FramePage.aspx?type=cateitem&itemcateid="+Eval("taobaocid").ToString() %>' target="_blank"  style=" font-size:small ; text-decoration:none;">宝贝列表</a>
                                        </td>
                                        <td class="list_td7">
                                            <%#GetMoveCateSelectHtml(Eval("taobaocid").ToString ())%>
                                            <input  type="hidden" value ='<%#Eval("taobaocid").ToString () %>'/>
                                        </td>
                                    </tr>
                                </table>
                                <div id='<%#Eval("taobaocid").ToString ()+"_detailDataList"%>' style =" width:100%;">
                                <asp:DataList ID="DetailDataList"  Width="100%" runat="server">
                                    <ItemTemplate>
                                        <table width="100%" >
                                            <tr>
                                                <td class="child_list_td0" style =" font-size:small;">
                                                    <a title="修改分类" href ="#4" id='<%#Eval("taobaocid").ToString () %>' onclick ="showRenameCateName(this);"><img   style=" border:none" src ="../../img/edititem.gif"/></a>
                                                    <span><%# Eval("taobaoname").ToString () %></span>
                                                    <input name="editCateNameTextInput" id='<%#Eval("taobaocid").ToString ()%>' title="回车键保存" type="text" value ="" style =" display:none;"/> 
                                                </td>
                                                <td class="list_td1">                                                 
                                                </td> 
                                                <td class="list_td2"></td>
                                                <td class="list_td3">                                                        
                                                    <%#getMoveUpAnchorHTML(Convert .ToInt32 (Eval ("taobaosort_order")),Eval("taobaocid").ToString (), Eval("taobaoparentcid").ToString())%>
                                                </td>
                                                <td class="list_td4">
                                                    <%#getMoveDownAnchorHTML(Convert.ToInt32(Eval("taobaosort_order")), Eval("taobaocid").ToString(), Eval("taobaoparentcid").ToString())%>
                                                </td>                                
                                                <td class="list_td5">
                                                    <a href="#" id='<%#Eval("taobaocid").ToString() %>' title="删除" onclick ="deleteCateId(this);" onmouseover="this.style.backgroundPosition='0px 0px';" onmouseout ="this.style.backgroundPosition='-23px 0px';" class="ItemCateOprButton" style=" background-position:-23px 0px;"></a>
                                                </td>
                                                <td class="list_td6">
                                                    <a href='<%#"FramePage.aspx?type=cateitem&parentcid="+Eval("taobaoparentcid").ToString()+"&itemcateid="+Eval("taobaocid").ToString() %>' target="_blank"  style=" font-size:small ; text-decoration:none;">宝贝列表</a>
                                                </td>
                                                <td class="list_td7">
                                                    
                                                </td>
                                            </tr>
                                        </table>
                                        <input  type ="hidden" name="exsitChildCateHidden"/>
                                    </ItemTemplate>
                                 </asp:DataList>
                                 </div>
                            </div>
                         </ItemTemplate>
                      </asp:DataList> 
                      
                      <%--添加分类--%>
                      <div id="addItemCateDiv" style=" display:none" >
                        <table width="100%" >
                            <tr>
                                <td class="list_td0" style =" font-size:small;">
                                    <input value="请输入分类" id="newCateIdText" type ="text"/>
                                </td>
                                <td class="list_td1">                                                 
                                </td> 
                                <td class="list_td2"></td>
                                <td class="list_td3">                                                        
                                    <a href="#" title="向上移动" class="ItemCateOprButton" style=" background-position:-23px -102px;cursor:not-allowed"> </a>
                                </td>
                                <td class="list_td4">
                                    <a href="#" title="向下移动"  class="ItemCateOprButton" style=" background-position:-23px -85px;cursor:not-allowed"> </a>
                                </td>                                
                                <td class="list_td5">
                                    <a href="#" title="取消" onclick ="hasNewFirstLevelCate=false;document.getElementById('addItemCateDiv').style.display ='none';" onmouseover="this.style.backgroundPosition='0px 0px';" onmouseout ="this.style.backgroundPosition='-23px 0px';" class="ItemCateOprButton" style=" background-position:-23px 0px;"></a>
                                </td>
                                <td class="list_td6">
                                    
                                </td>
                                <td class="list_td7">
                                    <%--<asp:Button ID="saveNewCateIdButton" CssClass ="saveNewCateButton" runat="server" Text="保存" OnClientClick ="return saveNewCateId();" />--%>
                                    <input  type ="button" value="保存" id="saveNewCateIdButton"  onclick ="saveFirstLevelNewCateId();"/>
                                </td>
                            </tr>
                        </table>
                      </div>
                      <%--添加子分类--%>
                      <div id="addChildItemCateDiv" style=" display:none" >
                        <table width="100%" >
                            <tr>
                                <td class="child_list_td0" style =" font-size:small;">
                                    <input value="请输入分类"  id="newChildCateIdText" type ="text"/>
                                </td>
                                <td class="list_td1">                                                 
                                </td> 
                                <td class="list_td2"></td>
                                <td class="list_td3">                                                        
                                    <a href="#" title="向上移动" class="ItemCateOprButton" style=" background-position:-23px -102px;cursor:not-allowed"> </a>
                                </td>
                                <td class="list_td4">
                                    <a href="#" title="向下移动"  class="ItemCateOprButton" style=" background-position:-23px -85px;cursor:not-allowed"> </a>
                                </td>                                
                                <td class="list_td5">
                                    <a href="#" title="取消" onclick ="hasNewSecondLevelCate=false;document.getElementById('addChildItemCateDiv').style.display ='none';" onmouseover="this.style.backgroundPosition='0px 0px';" onmouseout ="this.style.backgroundPosition='-23px 0px';" class="ItemCateOprButton" style=" background-position:-23px 0px;"></a>
                                </td>
                                <td class="list_td6">
                                    
                                </td>
                                <td class="list_td7">
                                    <%--<asp:Button ID="saveNewCateIdButton" CssClass ="saveNewCateButton" runat="server" Text="保存" OnClientClick ="return saveNewCateId();" />--%>
                                    <input  type ="button" value="保存" id="saveNewChildCateIdButton" onclick ="saveSecondLevelNewCateId();"/>
                                    <input  type ="hidden" id="newChildCateParentCidHidden" value =""/>
                                </td>
                            </tr>
                        </table>
                      </div>                       
                      <div class="itemcateFooterDiv" style="margin-top:2px; background-color:#ECECEC">                        
                           <span style=" float:left; width:20px;">&nbsp;&nbsp;&nbsp;&nbsp;</span><a class="add-button" onclick ="showFirstAddNewCateIdDiv();" href ="#1">添加新分类</a>
                      </div>
                      
                      <asp:Button ID="UpdatePageButton" CssClass="hiddenObject" runat="server" Text="刷新" 
                          onclick="UpdatePageButton_Click"  />
                  </div>
             </div>
        </div>                                                 
    </form>
    <script type ="text/javascript">
       
    
        var hasNewFirstLevelCate = false;
        var hasNewSecondLevelCate = false;
        var cateNameEditInputList =new Array ();
        function initFrameHeight() {
            if (window.parent != null) {
                document.getElementById("mainitemcateDiv").style.width = screen.availWidth - 220 + "px";
                var frameObj = window.parent.document.getElementById("CateOprFrame");
                if (frameObj != null) {
                    frameObj.style.height = document.getElementById("mainitemcateDiv").offsetHeight + 200 + "px";
                    frameObj.style.width = screen.availWidth - 200 + "px";
                } 
            }
        }
        window.onload = function() {
            initFrameHeight();
            initEditCateNameInputKeyDown();
        }

        function initEditCateNameInputKeyDown() {
            var editcatenameInputList = document.getElementsByName("editCateNameTextInput");
            for (var i = 0; i < editcatenameInputList.length; i++) {
                editcatenameInputList[i].onkeydown = editCateNameTextKeyDown;
            }
        }

        function showRenameCateName(thisObj) {
            var spanEle = thisObj.parentNode.getElementsByTagName("span")[0];
            var inputEle = thisObj.parentNode.getElementsByTagName("input")[0];
            var cateid = thisObj.id;
            var catename = spanEle.innerHTML;
            var beingEdit = false;
            if (inputEle.style.display == "none")
                beingEdit = false;
            else
                beingEdit = true;

            if (beingEdit == true) {
                var result = AjaxFunction.renameCateName(cateid, catename, inputEle.value);
                if (result.value != null && result.value == "") {
                    thisObj.title = "修改分类";
                    thisObj.getElementsByTagName("img")[0].src = "../../img/edititem.gif";
                    spanEle.style.display = "inline";
                    inputEle.style.display = "none";
                    spanEle.innerHTML = inputEle.value;
                    inputEle.value = "";
                    var length = cateNameEditInputList .length;
                    for (var i = 0; i < length; i++)
                        if (cateNameEditInputList[i].id == inputEle.id)
                            cateNameEditInputList.splice(i,1);
                }
                else {
                    if (result.value != null)
                        alert(result.value);
                } 
            }
            else {
                thisObj.title = "保存分类";
                thisObj.getElementsByTagName("img")[0].src = "../../img/saveitem.ico";
                spanEle.style.display = "none";
                inputEle.style.display = "inline";
                inputEle.value = spanEle.innerHTML;
                cateNameEditInputList.push(inputEle);
            }    
            
            //var result = AjaxFunction.renameCateName(cateid, catename, newcatename);             
        }

        function editCateNameTextKeyDown(o) {
            o = o || window.event;
            if (o.keyCode=="13") {
                var thisObj = o.target || o.srcElement;
                thisObj.parentNode.getElementsByTagName("a")[0].click();
            }
        }
            
        function moveCateSelectedChanged(thisObj) {
            if (validateOpr("移动分类") == false)
                return;
                
            var currentCateId = thisObj.parentNode.getElementsByTagName("input")[0].value;
            var moveAfterCateId = thisObj[thisObj.selectedIndex].value;
            var result = AjaxFunction.moveCateIdTo(currentCateId, moveAfterCateId);
            if (result.value != null && result.value == true) {
                updatePage()
            }
            else {
                alert("移动失败！");
            }
        }

        function moveCateId(targetCateId, isMoveDown) {
            if (validateOpr("移动分类") == false)
                return;
                
            var result = AjaxFunction.moveCateId(targetCateId, isMoveDown);
            if (result.value != null && result.value == true) {
                updatePage();
            }
            else {
                alert("移动失败！");
            }
        }
        //删除子分类，如果一个分类有子分类的话，就能删除
        function deleteCateId(thisObj) {
            if (validateOpr("删除分类") == false)
                return ;
            
            var exsitchildcate = exsitChildCates(thisObj.id);
            if (exsitchildcate==true ) {
                alert("对不起，该分类下面还有子分类，不能删除！");
                return;
            }
            var cateItemsResult = AjaxFunction.getItemsFromSellerCateId(thisObj.id);
            if (cateItemsResult.value != null ) {
                var deleteAble = false;
                if (cateItemsResult.value != "") {
                    var confirmResult = confirm("该分类中有多个商品，如果删除该分类，相应的商品会被移出该分类，确认删除？");
                    deleteAble = (confirmResult ==true );
                }
                else {
                    deleteAble = true;
                }
                if (deleteAble == true) {
                    var itemcid = thisObj.id;
                    var result = AjaxFunction.deleteCateId(itemcid);
                    if (result.value != null && result.value == "") {
                        updatePage();
                    }
                    else {
                        alert("删除失败:" + result.value);
                    }
                }
            }
        }

        //保存新建的一级分类
        function saveFirstLevelNewCateId() {
            var newCateIdText = document.getElementById("newCateIdText");
            if (newCateIdText.value == "" || newCateIdText.value == "请输入分类") {
                alert("请您重新输入您添加的分类的名称！");
            }
            else {
                var result = AjaxFunction.saveNewCateId(newCateIdText.value, "0");
                if (result.value != null && result.value == "") {
                    document.getElementById("addItemCateDiv").style.display = "none";
                    hasNewFirstLevelCate = false;
                    updatePage();
                    return true;
                }
                else {
                    alert("添加失败：" + result.value);
                    return false;
                }      
            }
        }
        //保存新建的二级分类
        function saveSecondLevelNewCateId() {
            var newCateIdText = document.getElementById("newChildCateIdText");
            if (newCateIdText.value == "" || newCateIdText.value == "请输入分类") {
                alert("请您重新输入您添加的分类的名称！");
            }
            else {
                var parentCid = document.getElementById("newChildCateParentCidHidden").value;
                var result = AjaxFunction.saveNewCateId(newCateIdText.value, parentCid);
                if (result.value != null && result.value == "") {
                    document.getElementById("addChildItemCateDiv").style.display = "none";
                    hasNewSecondLevelCate  = false;
                    updatePage();
                    return true;
                }
                else {
                    alert("添加失败：" + result.value);
                    return false;
                }
            }
        }

        //显示 添加新的一级分类
        function showFirstAddNewCateIdDiv() {
            var addItemCateDiv = document.getElementById("addItemCateDiv");
            addItemCateDiv.style.display = "block";
            document.getElementById("newCateIdText").value = "请输入分类";
            //document.getElementById("saveNewCateIdButton").style.display = "block";
            hasNewFirstLevelCate = true;
            document.getElementById("newCateIdText").focus();         
        }
        //显示 添加新的一级分类
        function showSecondAddNewCateIdDiv(parentCid) {
            var beMove = false;
            if (hasNewSecondLevelCate == true) {
                var confirmResult = confirm("已经存在新建的子级分类，是否要取消正在编辑的子分类？");
                if (confirmResult != true)
                    return;
                beMove = true;
            }
            var addchildItemCateDiv = document.getElementById("addChildItemCateDiv");
            addchildItemCateDiv.style.display = "block";
            document.getElementById("newChildCateIdText").value = "请输入分类";
            document.getElementById("newChildCateParentCidHidden").value = parentCid;
            if (beMove == true || hasNewSecondLevelCate ==false ) {
                var parentNewChildCateEle = addchildItemCateDiv.parentNode;
                var currentMainDataListDiv = document.getElementById(parentCid + "_mainDataList");
                parentNewChildCateEle.removeChild(addchildItemCateDiv);
                currentMainDataListDiv.appendChild(addchildItemCateDiv);
            }
            hasNewSecondLevelCate = true;
            document.getElementById("newChildCateIdText").focus();
        }

        function showAlertText(isalert) {
            var newClassName = (isalert==true )?"alertText":"";
            document.getElementById("newChildCateIdText").className = newClassName;
            document.getElementById("newCateIdText").className = newClassName;
            document.getElementById("addChildItemCateDiv").className = newClassName;
            document.getElementById("addItemCateDiv").className = newClassName;

            for (var i = 0; i < cateNameEditInputList.length; i++) {
                cateNameEditInputList[i].className = newClassName;
            }
        }
        function validateOpr(oprName) {
            showAlertText(true);
            if (hasNewFirstLevelCate == true || hasNewSecondLevelCate == true || cateNameEditInputList .length >0) {
                var confirmResult0 = confirm("您还有一些修改未保存，您确认要" + oprName + "并刷新页面吗？\n请“确认”继续，或按“取消”撤销" + oprName + "操作！");
                if (confirmResult0 != true) {
                    showAlertText(false);
                    return false ;
                } 
            }
            return true ;
        }
        
        function updatePage() {
            document.getElementById("UpdatePageButton").click();
        }

        //从页面上判断是否存在子分类
        function exsitChildCates(itemcateid) {
            var mainDataListItemDiv = document.getElementById (itemcateid + "_detailDataList");
            if (mainDataListItemDiv != null) {
                return (mainDataListItemDiv.innerHTML.replace(/\s/g, "") != "");
            }
            return false;
        }
        document.getElementById("newChildCateIdText").onkeydown = function(o) {
            o = o || window.event;
            if (o.keyCode == "13") {
                saveSecondLevelNewCateId();
            }
        }
        document.getElementById("newCateIdText").onkeydown = function(o) {
            o = o || window.event;
            if (o.keyCode == "13") {
                saveFirstLevelNewCateId();
            }
        }
    </script>
</body>
</html>
